##########################################
## Copyright 2012-2013 Ceruti Francesco
##
##
## This file is part of LiSP (Linux Show Player).
##
## LiSP (Linux Show Player) is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LiSP (Linux Show Player) is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with LiSP (Linux Show Player).  If not, see <http://www.gnu.org/licenses/>.
##########################################

from PyQt4 import QtCore
from PyQt4 import QtGui
from lisp.utils import utility
from lisp.ui.preferences.preferences_widget import PreferencesWidget
import re


class Appearance(PreferencesWidget):

    NAME = 'Appearance'

    DEFAULT_STILE = 'background-color: ' + utility.createGradient('rgba(170,170,170,170)') + '\
                     border: 1 solid rgba(150,150,150,255);\
                     border-radius: 6;'

    def __init__(self, size, parent=None):
        super(Appearance, self).__init__(size, parent)

        self.verticalLayoutWidget = QtGui.QWidget(self)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 431, 400))
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setMargin(0)

        self.groupBox_2 = QtGui.QGroupBox(self.verticalLayoutWidget)
        self.lineEdit = QtGui.QTextEdit(self.groupBox_2)
        self.lineEdit.setGeometry(QtCore.QRect(0, 25, 431, 70))
        self.verticalLayout.addWidget(self.groupBox_2)

        self.groupBox_5 = QtGui.QGroupBox(self.verticalLayoutWidget)
        self.spinBox = QtGui.QSpinBox(self.groupBox_5)
        self.spinBox.setGeometry(QtCore.QRect(0, 45, 200, 30))
        self.verticalLayout.addWidget(self.groupBox_5)

        self.groupBox_4 = QtGui.QGroupBox(self.verticalLayoutWidget)
        self.button_2 = QtGui.QPushButton(self.groupBox_4)
        self.button_2.setGeometry(QtCore.QRect(0, 40, 200, 40))
        self.verticalLayout.addWidget(self.groupBox_4)

        self.groupBox_3 = QtGui.QGroupBox(self.verticalLayoutWidget)

        self.button_4 = QtGui.QLabel(self.groupBox_3)
        self.button_4.setStyleSheet(self.DEFAULT_STILE)
        self.button_4.setAlignment(QtCore.Qt.AlignCenter)
        self.button_4.setGeometry(QtCore.QRect(20, 30, 160, 65))

        self.button_5 = QtGui.QLabel(self.groupBox_3)
        self.button_5.setStyleSheet(self.DEFAULT_STILE)
        self.button_5.setAlignment(QtCore.Qt.AlignCenter)
        self.button_5.setGeometry(QtCore.QRect(240, 30, 160, 65))

        self.verticalLayout.addWidget(self.groupBox_3)

        self.lineEdit.textChanged.connect(self.changeText)
        self.spinBox.valueChanged.connect(self.changeFontSize)
        self.button_2.clicked.connect(self.changeColor)

        self.horizontalLayoutWidget = QtGui.QWidget(self)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(40, 425, 390, 40))
        self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setMargin(0)

        self.warningImage = QtGui.QLabel(self.horizontalLayoutWidget)
        self.warningImage.setPixmap(QtGui.QIcon.fromTheme("dialog-warning").pixmap(QtCore.QSize(22, 22)))
        self.horizontalLayout.addWidget(self.warningImage)

        self.warning = QtGui.QLabel(self.horizontalLayoutWidget)
        self.warning.setText(" <b>The real appearance depends on the layout.</b>")
        self.warning.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed))
        self.horizontalLayout.addWidget(self.warning)

        self.retranslateUi()

        self.color = 'rgba(170,170,170,255)'
        self.spinBox.setValue(11)

    def retranslateUi(self):
        self.groupBox_2.setTitle("Shown Text")
        self.lineEdit.setText("Empty")
        self.groupBox_5.setTitle("Set Font Size")
        self.groupBox_4.setTitle("Button Color")
        self.button_2.setText("Select Color")
        self.groupBox_3.setTitle("Preview")
        self.button_4.setText("Previous")
        self.button_5.setText("Empty")

    def enableCheck(self, enable):
        self.groupBox_2.setCheckable(enable)
        self.groupBox_2.setChecked(False)

        self.groupBox_4.setCheckable(enable)
        self.groupBox_4.setChecked(False)

        self.groupBox_5.setCheckable(enable)
        self.groupBox_5.setChecked(False)

    def getConfiguration(self):
        conf = {}

        checked = self.groupBox_2.isCheckable()

        if(not (checked and not self.groupBox_2.isChecked())):
            conf['name'] = self.lineEdit.toPlainText()
        if(not (checked and not self.groupBox_4.isChecked())):
            conf['color'] = self.color
        if(not (checked and not self.groupBox_5.isChecked())):
            conf['font-size'] = self.spinBox.value()

        return conf

    def setConfiguration(self, conf):
        if(conf is not None):
            if('name' in conf):
                self.lineEdit.setText(conf['name'])
                self.button_5.setText(conf['name'])
            else:
                self.lineEdit.setEnabled(False)
            if('color' in conf):
                self.color = conf['color']
            else:
                self.button_2.setEnabled(False)
            if('font-size' in conf):
                self.spinBox.setValue(conf['font-size'])
            else:
                self.button_5.setEnabled(False)

            self.button_5.setStyleSheet('background-color: ' + utility.createGradient(self.color) + '\
                         border: 1 solid rgba(150,150,150,255);\
                         border-radius: 6;\
                         font-size: ' + str(self.spinBox.value()) + 'pt;')

    def changeFontSize(self, size):
        self.button_5.setStyleSheet('background-color: ' + utility.createGradient(self.color) + '\
                     border: 1 solid rgba(150,150,150,255);\
                     border-radius: 6;\
                     font-size: ' + str(size) + 'pt;')

    def changeColor(self):
        initial = QtGui.QColor(*map(int, re.findall('\d{1,3}', self.color)))
        color = QtGui.QColorDialog.getColor(initial, parent=self)

        if(color.isValid()):
            self.color = 'rgba' + str(color.getRgb())
            self.button_5.setStyleSheet('background-color: ' + utility.createGradient(self.color) + '\
                     border: 1 solid rgba(150,150,150,255);\
                     border-radius: 6;\
                     font-size: ' + str(self.spinBox.value()) + 'pt;')

    def changeText(self):
        self.button_5.setText(self.lineEdit.toPlainText())
